package LifeForms;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.rmi.server.ExportException;
import java.util.ArrayList;

import javax.management.timer.Timer;

import Game.Entity;
import Game.Explosion;
import Game.IndexCounter;
import Game.Mainth;
import Game.Sprite;
import Guns.Cal45;
import Guns.Projectile;
import Levels.Objects;
import SoundSystem.Sound;

public abstract class Robot extends Entity{
	private double armor;
	private double firePower;
	private double power=100;
	private double speed;
	private int ammo=100;
	private int missles;
	boolean toggle=false;
	boolean q=false;
	public Turret turret=new Turret(this, Objects.turretSprite, 2, getX(), getY());;
	IndexCounter index=new IndexCounter(4,1);
	
	public Robot(Sprite sprite, int ID, int x, int y) {
		super(sprite, ID, x, y);
	}
	public int getIndex(){
		return index.getIndex();
	}
	public void setPower(double power){
		this.power=power;
	}
	public void subtractPower(double power){
		this.power-=power;
	}
	public void addpower(double power){
		this.power+=power;
	}
	public double getPower(){
		return power;
	}
	public void setArmor(double armor){
			this.armor=armor;
	}
	public void subtractArmor(double armor){
		this.armor-=armor;
	}
	public void addArmor(double armor){
		this.armor+=armor;
	}
	public double getArmor(){
		return armor;
	}
	public void setFirePower(double firePower){
		this.firePower=firePower;
	}
	public void subtractFirePower(double firePower){
		this.firePower-=firePower;
	}
	public void addFirePower(double firePower){
		this.firePower+=firePower;
	}
	public double getFirePower(){
		return firePower;
	}
	public void setSpeed(double speed){
		this.speed=speed;
	}
	public void subtractSpeed(double speed){
		this.speed-=speed;
	}
	public void addSpeed(double speed){
		this.speed+=speed;
	}
	public double getSpeed(){
		return speed;
	}
	public void setAmmo(int ammo){
		this.ammo=ammo;
	}
	public void subtractAmmo(int ammo){
		this.ammo-=ammo;
	}
	public void addAmmo(int ammo){
		this.ammo+=ammo;
	}
	public double getAmmo(){
		return ammo;
	}
	public void setMissles(double Missles){
		this.missles=missles;
	}
	public void subtractMissles(double missles){
		this.missles-=missles;
	}
	public void addMissles(double missles){
		this.missles+=missles;
	}
	public double getMissles(){
		return missles;
	}
	public double getfireSpeedX(){
		int sub=0;
		int div=90;
		if(angle>=0&&angle<180){
			if(angle<90){
				sub=0;
				div=90;
			}else if(angle<180){
				sub=180;
				div=90;
			}
		}else if(angle>=180&&angle<360){
			if(angle<270){
				sub=180;
				div=-90;
			}else if(angle<360){
				sub=360;
				div=-90;
			}
		}
		
		return ((double)(Math.abs(angle-sub))/div);
	}
	public double getfireSpeedY(){
		int sub=0;
		int div=90;
		if(angle>=90&&angle<270){
			if(angle<180){
				sub=90;
				div=90;
			}else if(angle<270){
				sub=270;
				div=-90;
			}
		}else if(angle>=270||angle<90){
			if(angle<90){
				sub=90;
				div=90;
			}else if(angle<360){
				sub=270;
				div=-90;
			}
		}
		return ((double)(angle-sub)/div);
	}
	public int getAngle(){
		return angle;
	}
	public int getDrive(){
		int dir=0;
		if(angle<=315&&angle>225){
			dir=1;
		}
		if(angle<=45||angle>315){
			dir=2;
		}
		if(angle<=135&&angle>45){
			dir=3;
		}
		if(angle<=225&&angle>135){
			dir=4;
		}
		return dir;
	}
	public void move(){
		this.x+=dy*getfireSpeedX();
		this.y+=dy*getfireSpeedY();
	}
	public void death(ArrayList<Robot> robots,ArrayList<Explosion> expo,Sprite _sprite){
		if(this.power<1){
			robots.remove(this);
			Explosion explo=new Explosion(_sprite,expo,(int)x,(int)y);
			expo.add(explo);
		}
	}
	public void control(ArrayList<Projectile> projectiles){
		if(Mainth.q&&!q){
			toggle=!toggle;
			q=true;
		}if(!Mainth.q){
			q=false;
		}
		if(toggle){
			if(Mainth.left){
				angle-=1;
			}if(Mainth.right){
				angle+=1;
			}if(Mainth.up){
				setVerMove(1);
			}
			else if(Mainth.down){
				setVerMove(-1);
			}else{
				setVerMove(0);
			}
		}
	}
	public void update(ArrayList<Projectile> projectiles){	
		move();
		if(angle>=360){
			angle-=360;
		}
		if(angle<0){
			angle+=360;
		}
		if(getVerMove()!=0){
        	index.Counter();
        }
	_update();
	turret.update(this);
	turret.control(projectiles,this);
	}
	public void powerBar(double power,Graphics2D g2d){
		Rectangle powerbar=new Rectangle((int)this.x,(int)this.y-30,(int)(power/2.4),5);
		g2d.setColor(Color.RED);
		g2d.fill(powerbar);
	}
	public void paint(Graphics g,int x,int y){
		_paint(g);
	}
	public void paint(Graphics2D g2d,int x,int y){
		Rectangle powerbarframe=new Rectangle((int)this.x,(int)this.y-30,(int)(100/2.4),5);
		powerBar(power,g2d);
		g2d.draw(powerbarframe);
		_paint(g2d,x,y);
		turret.paint(g2d,0,x,y);
	}
	abstract void _update();
	abstract void _paint(Graphics g);
	abstract void _paint(Graphics2D g2d,int x,int y);
}
